package com.fashion.server.service.impl;

import com.fashion.server.common.IConstants;
import com.fashion.server.dao.MapperFactory;
import com.fashion.server.domain.model.IdentityOfficialModel;
import com.fashion.server.domain.model.WebUserModel;
import com.fashion.server.domain.request.HttpDataParam;
import com.fashion.server.domain.response.ResponseData;
import com.fashion.server.service.WebUserService;
import com.fashion.server.utils.JsonUtil;
import com.fashion.server.utils.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by zhuqi on 2016/9/6.
 */
@Service("webUserService")
public class WebUserServiceImpl implements WebUserService {

    private static final Logger logger = Logger.getLogger(WebUserServiceImpl.class);

    @Resource
    private MapperFactory mapperFactory;

    @Override
    public String queryWebUsers(WebUserModel webUserModel) {

        if(webUserModel.getFields().contains("identity.")){
            webUserModel.setQueryTable1(1);
        }
        //只查询count数
        if(webUserModel.onlyQueryCount()){
            ResponseData responseData = new ResponseData();
            responseData.setCode(IConstants.ResponseDefine.Code.SUCCESS_200.getIndex());
            responseData.setData(mapperFactory.getReadWebUserMapper().selectByObejctPaging(webUserModel));
            return JsonUtil.toJSONString(responseData, false);
        }

        ResponseData responseData = new ResponseData();
        //是否需要返回totalCount
        if(webUserModel.needTotalCount()){
            responseData.setTotalCount(mapperFactory.getReadWebUserMapper().selectCountByObject(webUserModel));
        }
        List<WebUserModel> webUserList = null;
        if(webUserModel.getFields().contains("authId")){
            webUserList = mergeIdentityList(mapperFactory.getReadWebUserMapper().selectByObejctPaging(webUserModel));
        }else{
            webUserList = mapperFactory.getReadWebUserMapper().selectByObejctPaging(webUserModel);
        }

        if (webUserList.isEmpty()) {
            responseData.setCode(IConstants.ResponseDefine.Code.SUCCESS_200.getIndex());
            responseData.setMsg("没有查询到数据");
            responseData.setData(new ArrayList<WebUserModel>());
        } else {
            responseData.setData(webUserList);
        }
        return JsonUtil.toJSONString(responseData, false);
    }


    @Override
    public String addWebUser(HttpDataParam<WebUserModel> dataCondition) {
        List<WebUserModel> list = dataCondition.getObjects();
        ResponseData responseData = new ResponseData();
        Date date = new Date();
        if(null != list){
            for (WebUserModel model : list) {
                model.setModifyTime(date);
                mapperFactory.getWriteWebUserMapper().insertSelective(model);
            }
            responseData.setData(null);
        }else{
            responseData.setCode(IConstants.ResponseDefine.Code.ERROR_402.getIndex());
            responseData.setMsg("参数为空");
        }

        return JsonUtil.toJSONString(responseData, false);
    }
    
    @Override
    public String updateWebUser(HttpDataParam<WebUserModel> dataCondition) {
        List<WebUserModel> list = dataCondition.getObjects();
        ResponseData responseData = new ResponseData();
        Date date = new Date();
        if(null != list){
            // 参数校验
            for (WebUserModel model : list) {
                ResponseData checkData = checkUpdateData(model);
                checkData.setData(model);
                if (IConstants.ResponseDefine.Code.SUCCESS_200.getIndex() != checkData.getCode()) {
                    return JsonUtil.toJSONString(checkData, false);
                }
            }
            for (WebUserModel model : list) {
                model.setModifyTime(date);
                mapperFactory.getWriteWebUserMapper().updateByPrimaryKeySelective(model);
            }
            responseData.setData(null);
        }else{
            responseData.setCode(IConstants.ResponseDefine.Code.ERROR_402.getIndex());
            responseData.setMsg("参数为空");
        }

        return JsonUtil.toJSONString(responseData, false);
    }

    private ResponseData checkUpdateData(WebUserModel object) {
        ResponseData responseData = new ResponseData();
        responseData.setCode(IConstants.ResponseDefine.Code.ERROR_402.getIndex());
        responseData.setData(object);
        if (null == object.getId() && StringUtil.isBlank(object.getUserId())) {
            responseData.setMsg("参数错误:Id和userId最少有一个");
            return responseData;
        }
        return new ResponseData(IConstants.ResponseDefine.Code.SUCCESS_200.getIndex());
    }

    //合并主播认证身份信息
    private List<WebUserModel> mergeIdentityList(List<WebUserModel> webUserModels){
        List<Long> ids = new ArrayList<Long>();
        for(WebUserModel webUserModel:webUserModels){
            if(webUserModel.getAuthId() > 0){
                ids.add(webUserModel.getAuthId().longValue());
            }
        }
        if(ids == null || ids.size() == 0){
            return webUserModels;
        }

        List<IdentityOfficialModel> identityOfficialModels = mapperFactory.getReadIdentityOfficialMapper().selectByPrimaryKeys(ids);
        Map<Long,String> map = new HashMap<>();
        for(IdentityOfficialModel identityOfficialModel : identityOfficialModels){
            map.put(identityOfficialModel.getId(),identityOfficialModel.getName());
        }

        for (WebUserModel userModel:webUserModels){
            if(map.containsKey(userModel.getAuthId().longValue())){
                userModel.setAuthInfo(map.get(userModel.getAuthId().longValue()));
            }
        }
        return webUserModels;
    }
}
